package gis.uzd_1;

import java.awt.Rectangle;

import org.geotools.swing.event.MapMouseEvent;
import org.geotools.swing.tool.CursorTool;

/**
 * Map selection cursor tool.
 * 
 * @version 0.1
 * @date 2010-04-24
 * @author Edmundas Matusevicius
 */
public class SelectCursorTool extends CursorTool {
    @Override
    public void onMousePressed(MapMouseEvent e) {
        startX = e.getX();
        startY = e.getY();
    }
    
    @Override
    public void onMouseReleased(MapMouseEvent e) {
        endX = e.getX();
        endY = e.getY();
        
        if (GISMenu.viewState == GISMenu.ViewState.ZOOM_IN) {
            dragged = false;
        } else if (GISMenu.viewState == GISMenu.ViewState.SELECT) {
            dragged = false;
        }
    }
    
    @Override
    public void onMouseDragged(MapMouseEvent e) {
        if (GISMenu.viewState == GISMenu.ViewState.ZOOM_IN) {
            dragged = true;
        } else if (GISMenu.viewState == GISMenu.ViewState.SELECT) {
            dragged = true;
        } else {
            dragged = false;
        }
    }
    
    @Override
    public boolean drawDragBox() {
        return true;
    }
    
    public Rectangle getBoundsRectangle() {
        Rectangle rect = null;
        
        if (startX <= endX) {
            if (startY <= endY) {
                rect = new Rectangle(startX, startY, endX - startX, endY - startY);
            } else {
                rect = new Rectangle(startX, endY, endX - startX, startY - endY);
            }
        } else {
            if (startY <= endY) {
                rect = new Rectangle(endX, startY, startX - endX, endY - startY);
            } else {
                rect = new Rectangle(endX, endY, startX - endX, startY - endY);
            }
        }
        
        return rect;
    }
    
    @SuppressWarnings("unused")
    private boolean dragged;
    
    private int startX;
    private int startY;
    private int endX;
    private int endY;
}
